package com.banciyuan.bcywebview.biz.main.mineinfo.reward;

import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.GiftApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.post.Listener.OnTextChangeListener;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.PayStatus;

/**
 * Created by squall on 2016/5/31.
 */
public class ChangeRewardActivity extends BaseActivity {

    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private TextView tv_length;
    private EditText content_et;
    private boolean click_flag = false;
    private RequestQueue queue;
    private PayStatus payStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changefeedintro);
        initArgs();
        initActionbar();
        initUi();
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(getContext());
        payStatus = (PayStatus) getIntent().getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.feed_des));
        mActionbarHelper.addItemOneText(getString(R.string.save));
        TextView save = (TextView) mActionBarView.findViewById(R.id.base_action_bar_home_text_item);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!click_flag) {
                    setintro();
                }
            }
        });
    }

    @Override
    protected void initUi() {
        tv_length = (TextView) findViewById(R.id.show_length);
        content_et = (EditText) findViewById(R.id.et_desc);

        content_et.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                tv_length.setText(s.length() + "/30");
            }
        });
        if (payStatus != null && !TextUtils.isEmpty(payStatus.getIntro())) {
            content_et.setText(Html.fromHtml(payStatus.getIntro()));
        }
    }

    private void setintro() {
        click_flag = true;
        String url = HttpUtils.BASE_URL + GiftApi.getSetPayInfo();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair(HttpUtils.GROUP_INTRO, content_et.getText().toString().trim()));
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                click_flag = false;
                if (StatusCodeUtils.dealStatusCode(response, getContext())) {
                    finish();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                click_flag = false;
            }
        };
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        queue.add(requestParam);
    }
}
